﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using ITProject.Domain.Entities;

namespace ITProject.WebUI.Models.ProjectAdmin
{
    public class ProjectAdminEditTaskViewModel
    {
        [Display(Name = "Nazwa")]
        [Required(ErrorMessage = "Nazwa zadania jest wymagana")]
        [MinLength(4, ErrorMessage = "Nazwa zadania musi posiadać conajmniej 4 znaki")]
        [MaxLength(50, ErrorMessage = "Nazwa zadania nie może posiadać więcej niż 50 znaków")]
        public string Name { get; set; }
        [Display(Name = "Opis")]
        public string Description { get; set; }
        [Display(Name = "Planowana data ukończenia zadania")]
        [Required(ErrorMessage = "Podanie planowanej daty ukończenia jest wymagane")]
        public DateTime PlannedEndDate { get; set; }
        [Display(Name = "Priorytet")]
        [Required(ErrorMessage = "Musisz wybrać priorytet zadania")]
        public string ReturnPriority { get; set; }
        [Display(Name = "Uczestnik odpowiedzialny za zadanie")]
        public string ReturnUser { get; set; }
        [Display(Name = "Przypisanie do funkcjonalności")]
        public string ReturnFeature { get; set; }

        //ustawiane przez kontroler i ukryte
        public string ProjectName { get; set; }
        public int TaskId { get; set; }

        public PriorityViewModel PriorityList { get; set; }
        public List<SelectListItem> FeatureList;
        public List<SelectListItem> UserList;

        public static ProjectAdminEditTaskViewModel Convert(Task task, string projectName)
        {
            ProjectAdminEditTaskViewModel vm = new ProjectAdminEditTaskViewModel();
            vm.Name = task.Name;
            vm.PlannedEndDate = task.PlannedStopDate;
            vm.ProjectName = projectName;
            vm.Description = task.Description;
            vm.TaskId = task.Id;
            vm.PriorityList = new PriorityViewModel(PriorityViewModel.ConvertFromString(PriorityViewModel.ConvertFromByte(task.Priority)));
            return vm;
        }

        public static Task Convert(ProjectAdminEditTaskViewModel vm, int userId, int? featureId)
        {
            Task task = new Task();
            task.Id = vm.TaskId;
            task.Name = vm.Name;
            task.PlannedStopDate = vm.PlannedEndDate;
            task.Priority = (byte)PriorityViewModel.ConvertFromString(vm.ReturnPriority);
            task.Description = vm.Description;
            task.FeatureId = featureId;
            task.UserId = userId;
            // projectId, startDate i status zostaja bez zmian
            return task;
        }
    }
}